package com.wing.order.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.utils.ExcelUtils;
import com.wing.common.utils.PageResult;
import com.wing.web.db.config.util.DynamicDataSource;
import com.wing.order.model.entity.CabInventoryProductRecord;
import com.wing.order.model.response.CabInventoryProductRecordVO;
import com.wing.order.service.CabInventoryProductRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Slf4j
@Validated
@Api(value = "Admin-产品库存入库变动记录管理", tags = "#/order/inventoryProductRecordIn")
@RestController("inventoryProductRecordInController")
@RequestMapping("order/admin/inventoryProductRecordIn")
@RequiredArgsConstructor
public class InventoryProductRecordInController {

    private final CabInventoryProductRecordService cabInventoryProductRecordService;

    @ApiOperation(value = "查询所有产品库存入库变动记录", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<CabInventoryProductRecordVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
            ,String skuCode
            ,Long warehouseId
            ,String startCreateTime
            ,String endCreateTime
            ,String query
    ){

        Page<CabInventoryProductRecordVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();
        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", StrUtil.toUnderlineCase(sort));
            params.put("order", order);
        }
        if(StringUtils.isNotBlank(skuCode)){
            params.put("skuCode", skuCode);
        }
        if(null != warehouseId){
            params.put("warehouseId", warehouseId);
        }
        if (StringUtils.isNotBlank(startCreateTime)) {
            params.put("startCreateTime", startCreateTime);
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            params.put("endCreateTime", endCreateTime);
        }
        params.put("type",CabInventoryProductRecord.Type.in.getValue());
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if(StringUtils.isNotBlank(DynamicDataSource.name.get())){
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        params.put("databaseUser", databaseUser);
        if (StringUtils.isNotBlank(query)){
            params.put("query",query);
        }
        //搜索
        IPage<CabInventoryProductRecordVO> list = cabInventoryProductRecordService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response, @RequestParam String startCreateTime, @RequestParam String endCreateTime){
        Map paramsMap = new HashMap();
        paramsMap.put("startCreateTime", startCreateTime);
        paramsMap.put("endCreateTime", endCreateTime);
        paramsMap.put("type", CabInventoryProductRecord.Type.in);
        String databaseUser = DynamicDataSource.USER_PREFIX;
        if(StringUtils.isNotBlank(DynamicDataSource.name.get())){
            databaseUser = databaseUser + "-" + DynamicDataSource.name.get();
        }
        paramsMap.put("databaseUser", databaseUser);
        List<CabInventoryProductRecordVO> list = cabInventoryProductRecordService.list4app(paramsMap);
        list.parallelStream().forEach(k -> {
            k.setTypeDesc(k.getType().getDesc());
        });
        ExcelUtils.export(response, "入库记录列表", list, CabInventoryProductRecordVO.class);
    }

}
